package com.stedu.mall.orderservice.controller;


import com.github.pagehelper.PageInfo;
import com.stedu.mall.common.bean.Order;
import com.stedu.mall.common.bean.OrderDetail;
import com.stedu.mall.common.bean.OrderVo;
import com.stedu.mall.common.bean.RespBean;
import com.stedu.mall.common.exception.SteduException;
import com.stedu.mall.common.service.OrderDetailService;
import com.stedu.mall.common.service.OrderService;
import com.stedu.mall.common.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderDetailService orderDetailService;

    //添加
    @PostMapping
    public RespBean insert(@RequestBody OrderVo orderVo ,@RequestHeader("token") String token) throws SteduException {
        Map<String,Object> map = JwtUtil.parseJwtToMap(token);
        Integer userId = (Integer) map.get("id");
        orderVo.setUserId(userId);
        orderService.insert(orderVo);
        return RespBean.ok("添加成功", orderVo);
    }
    //删除
    @DeleteMapping("/{id}")
    public RespBean delete(@PathVariable("id") String id) throws SteduException {
        orderService.delete(id);
        return RespBean.ok("删除成功");
    }
    //修改
    @PutMapping
    public RespBean update(@RequestBody Order order) throws SteduException {
        orderService.update(order);
        return RespBean.ok("修改成功");
    }
    //分页搜索
    @GetMapping("/search")
    public RespBean selectByCondition(Order order, Integer pageNum, Integer pageSize){
        PageInfo<Order> pageInfo = orderService.selectByCondition(order, pageNum, pageSize);
        return RespBean.ok("", pageInfo);
    }
    //用户获取订单列表
    @GetMapping("/user")
    public RespBean selectByUserId(Integer pageNum, Integer pageSize, @RequestHeader("token") String token) throws SteduException {
        //解析token获取用户id
        Map<String, Object> map = JwtUtil.parseJwtToMap(token);
        Integer userId = (Integer)map.get("id");
        Order condition = new Order();
        condition.setUserId(userId);
        PageInfo<Order> orderPageInfo = orderService.selectByCondition(condition, pageNum, pageSize);
        return RespBean.ok("查询成功", orderPageInfo);
    }

    //根据id查询
    @GetMapping("/{orderId}")
    public RespBean selectById(@PathVariable("orderId") String id, @RequestHeader("token") String token) throws SteduException {
        //不需要获取jwt,直接解析
        Map<String, Object> map = JwtUtil.parseJwtToMap(token);
        //获取用户id
        Integer userId = (Integer) map.get("id");

        //查询订单信息
        Order order = orderService.selectById(id, userId);
        return RespBean.ok("根据id查询订单成功", order);
    }

    //支付
    @com.situ.mall.orderservice.annotation.Log(name = "订单支付记入日志")
    @PostMapping("/pay")
    public RespBean pay(@RequestBody OrderVo orderVo, @RequestHeader("token") String token) throws SteduException {
        //解析token获取用户id
        Map<String, Object> map = JwtUtil.parseJwtToMap(token);
        Integer userId = (Integer)map.get("id");
        orderVo.setUserId(userId);
        orderService.pay(orderVo);
        return RespBean.ok("支付成功");
    }
    //获取省份及订单量
    @GetMapping("/proCount")
    public RespBean selectProCount() {
        List<Map<String, Integer>> mapList = orderService.selectProCount();
        return RespBean.ok("", mapList);
    }
    @GetMapping("/firstEchart")
    public RespBean selectFive(){
        List<OrderDetail> data = orderDetailService.selectFive();
        return RespBean.ok("", data);
    }
    //收货
    @PutMapping("/shipping")
    public RespBean shipping(@RequestBody Order order) throws SteduException {
        String update = orderService.updategood(order);
        return RespBean.ok(update);
    }
}
